﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SomethingSpacialClassLib.Web;
using System.ServiceModel.DomainServices.Client;

namespace SomethingSpacial.Views.MixItUp
{
    public partial class HomePage : UserControl
    {

        public HomePage()
        {
            InitializeComponent();
			this.Loaded+=new System.Windows.RoutedEventHandler(HomePage_Loaded);
        }

        private void HomePage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            setUserGreeting();
        }

        void setUserGreeting() {

            /*TODO - AppContext retrieved empty*/
            string userName = ""; //ds 0421  AppServices.WebContext.Current.User.FriendlyName; 
            string userGreeting = (!string.IsNullOrEmpty(userName)) ? string.Format("Hello {0},", userName) : "Hello,";
            textBlockUserHeading.Text = userGreeting;


            var ctx = new SomethingSpacialContext();
            EntityQuery<Event> latestEventQuery = ctx.GetEventLatestQuery();
            var loadOperation = ctx.Load(latestEventQuery);


            if (!loadOperation.HasError)
                loadOperation.Completed += (s, ev) =>
                {
                    Event evt = loadOperation.Entities.SingleOrDefault();
                    if (evt != null)
                    {
                        textBlockUserGroupDescription.Text = evt.EVT_Description;
                        textBlockNextMeeting.Text = evt.EVT_Name;
                        TimerDisplay.SetDateTime(evt.EVT_Start_Date);
                    }
                };
            
        }
    }
}
